package com.eenie.onecloud.utils;

import java.security.MessageDigest;

public class MD5Utils {

    public static final String SIGN_SPLIT_SYMBOL = "&";


    public static String pwdMd5(String str) {
        StringBuilder stringBuilder = new StringBuilder(MD5(str));
        char charAt = stringBuilder.charAt(2);
        char charAt2 = stringBuilder.charAt(8);
        char charAt3 = stringBuilder.charAt(17);
        char charAt4 = stringBuilder.charAt(27);
        stringBuilder.deleteCharAt(27);
        stringBuilder.deleteCharAt(17);
        stringBuilder.deleteCharAt(8);
        stringBuilder.deleteCharAt(2);
        stringBuilder.insert(2, charAt2);
        stringBuilder.insert(8, charAt);
        stringBuilder.insert(17, charAt4);
        stringBuilder.insert(27, charAt3);
        return MD5(stringBuilder.toString());
    }


    public static String MD5(String str) {
        return byteToHexStr(getMD5Byte(str.getBytes()));
    }

    private static byte[] getMD5Byte(byte[] bArr) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(bArr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return messageDigest == null ? new byte[0] : messageDigest.digest();
    }


    private static String byteToHexStr(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer(bArr.length * 2);
        for (byte b : bArr) {
            String toHexString = Integer.toHexString(b & 255);
            if (toHexString.length() == 1) {
                stringBuffer.append("0").append(toHexString);
            } else {
                stringBuffer.append(toHexString);
            }
        }
        return stringBuffer.toString();
    }


}
